package com.sky.service.impl;

import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.interceptor.JwtTokenUserInterceptor;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShoppingMapper;
import com.sky.service.Orderservice;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class OrederServiceImpl implements Orderservice {
  @Autowired
          //订单表
  OrderMapper orderMapper;

  @Autowired
    AddressBookMapper addressBookMapper;
  @Autowired
    ShoppingMapper shoppingMapper;
  //订单明细表
    @Autowired
  OrderDetailMapper orderDetailMapper;
    @Override
    @Transactional
    public OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO) {

        //向订单表传数据
        Long userid = JwtTokenUserInterceptor.threadLocal.get();
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersSubmitDTO,orders);
        orders.setOrderTime(LocalDateTime.now());
        //设置支付状态 0表示未支付，1代表已支付
        orders.setPayStatus(Orders.UN_PAID);
        //设置订单状态
        orders.setStatus(Orders.PENDING_PAYMENT);
        //设置订单号(使用时间加用户id)
        orders.setNumber(String.valueOf(System.currentTimeMillis())+userid);
        //设置用户手机号
        //1.先获取用户手机号通过地址id
        AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
        //2.添加手机号
        orders.setPhone(addressBook.getPhone());
        //设置收货人名字
        orders.setConsignee(addressBook.getConsignee());
        //设置用户id
        orders.setUserId(userid);
        //添加到订单表中(在插入时要获取该id，后面订单详情要用)
        orderMapper.insertorder(orders);
        //向订单明细表插入n数据（就是菜品加订单id）
        //先通过用户id查购物车
        ShoppingCart shoppingCart  = new ShoppingCart();
        shoppingCart.setUserId(userid);
        //查询用户的购物车
         List<ShoppingCart> list = shoppingMapper.select(shoppingCart);
         List<OrderDetail> orderDetails  = new ArrayList<>();
         for (ShoppingCart cart : list)
         {
             OrderDetail orderDetail = new OrderDetail();
             BeanUtils.copyProperties(cart,orderDetail);
             //设置订单id(前面在加如时就已经返回)
             orderDetail.setOrderId(orders.getId());
             orderDetails.add(orderDetail);
         }
         //批量插入数据到订单明细表中
         orderDetailMapper.insertDetail(orderDetails);
         //清空购物车
        shoppingMapper.deleteById(shoppingCart);
         //返回数据
        OrderSubmitVO orderVO  =new OrderSubmitVO();
        orderVO.setOrderAmount(orders.getAmount());
        orderVO.setOrderNumber(orders.getNumber());
        orderVO.setOrderTime(orders.getOrderTime());
        orderVO.setId(orders.getId());
        return orderVO;
    }
}
